<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Redis;


class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    //接收邮件邮箱地址
    private $_to = '';

	//接收邮件邮箱标题
    private $_subject = '';

	/**
	 * 设置用户缓存
	 *
	 * @param array $user_info
	 * @return bool
	 */
    protected function buildSession($user_info = array()){
    	if(empty($user_info)){
    		return false;
		}
		session(['is_login'=>true, 'user_id'=>$user_info['user_id']]);
		Redis::set('user_info_'.$user_info['user_id'], json_encode($user_info));
    	return true;
	}

	/**
	 * 发送邮件
	 *
	 * @param $to
	 * @param array $data
	 * @param string $subject
	 * @return bool
	 */
	protected function sendMail($to, $data = [], $subject = ''){
		$this->_to = $to;
		$this->_subject = $subject;
		$result = Mail::send('email.template', $data, function ($send){
			$send->to($this->_to)->subject($this->_subject);
		});
		if($result){
			return true;
		}
		return false;
	}
}
